package br.com.daniele.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import br.com.daniele.bean.Det;
import br.com.daniele.bean.InfNFe;

public class GeraPDFServlet extends HttpServlet implements Servlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		
	    try {
	    	
	        JasperPrint print = null;

	        //chamando um servico que retorna a lista do bean que eu quero
	        Collection<Det> ls = new ArrayList<Det>();
	        
	        InfNFe infNFe = new InfNFe();
	        infNFe.setID("asdadhada4a5da46");
	        
	        Det det = new Det();
	        det.setId(500);
	        det.setInfAdProd("TESTE_setInfAdProd");
	        det.setnItem(1313);        
	        
	        
	        
	        Map<String, Object> parameters = new HashMap<String, Object>();
	        //passando um bean como parametro para o relatorio 
	        //tem que existir no relatorio um campo do mesmo tipo que estou passando 
	        //ou seja uma no relatorio o campo 'dados' deve ser do tipo Collection<MeuBean>
	        parameters.put("Det", det);
	        parameters.put("id", det.getId());
	        parameters.put("infAdProd", det.getInfAdProd());
	        parameters.put("id", det.getnItem());	        
	        
	        print = JasperFillManager.fillReport(getClass().getResourceAsStream("/br/com/daniele/relatorio/Det.jasper"), parameters, new JREmptyDataSource());
	        /*print = JasperFillManager.fillReport(getClass().getResourceAsStream("/br/com/pacotes/relatorios/relatorio.jasper"), parameters, new JREmptyDataSource());*/
	        
	        
	        FileOutputStream fileOutputStream = new FileOutputStream( "C:/eclipse/FUNCIONA.pdf") ;
			
			JRExporter exporter = new JRPdfExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fileOutputStream);
			exporter.exportReport();
			
			fileOutputStream.flush();
			fileOutputStream.close();
	        System.out.println("Ok");
	        
	    } catch (Exception ex) {
	        ex.printStackTrace();
	        throw new ServletException(ex);
	    }
		
		
		
	}
	

}
